/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-01-11     Wangjiangbo       the first version
 */
#include <app/app_dctft.h>
#include <app/app_motorcontrol.h>
#include <app/app_arm_control.h>
#include <app/app_eleact.h>
#include <app/app_force_move.h>
#include <app/app_io.h>
#include "DCTft/DCMasterDevice.h"
#include "mod/mod_cia402dev.h"

#define DBG_TAG "dctft"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

typedef enum {
    MAIN_WIDGET = 0,
    DEBUG_WIDGET,
    MOVE_WIDGET,
    SET_WIDGET,
    LOCKED_WIDGET,
} eui_t;

static eui_t _widget = MAIN_WIDGET;

static int16_t pallet_speed_value = 1000;
static uint8_t pallet_assit_status=0;

uint8_t pallet_locked = 0;

static bool proc(char*** p_msg, uint8_t*** p_param, uint16_t* param_len)
{
    const char* msg = 0;
    const uint8_t* param = 0;

    if(p_msg == NULL || *p_msg == NULL || **p_msg == NULL)
        return false;

    msg = **p_msg;

    if(p_param != NULL && *p_param != NULL && **p_msg != NULL )
        param = **p_param;

    LOG_D("msg in %s", msg);

    switch(_widget)
    {
    case MAIN_WIDGET:
        if(strcmp(msg, "切换到调试") == 0)
        {
            app_mc_enable();
//            app_eleact_enable();
            _widget = DEBUG_WIDGET;
        }
        else if(strcmp(msg, "切换到移动") == 0)
        {
            app_brake_state_set(1);
            cia402_set_output(LEFT_ID,2);
            cia402_set_output(RIGHT_ID,2);
            if(pallet_assit_status)app_force_move_enable();
            _widget = MOVE_WIDGET;
        }
        else if(strcmp(msg, "切换到设置") == 0)
        {
            app_arm_enable();
            _widget = SET_WIDGET;
        }
        else if(strcmp(msg, "台车锁定") == 0)
        {
            pallet_locked = 1; //台车锁定
        }
        else if(strcmp(msg, "台车解锁") == 0)
        {
            pallet_locked = 0; //台车锁定
        }
        break;

    case DEBUG_WIDGET:
        if(strcmp(msg, "切换到主界面") == 0)
        {
            app_brake_state_set(0);
            app_mc_disable();
//            app_eleact_disable();
            _widget = MAIN_WIDGET;
        }
        else if(strcmp(msg, "台车上升") == 0)
        {
//            app_eleact_run(1);
            app_mc_run(LIFT_ID, pallet_speed_value);
        }
        else if(strcmp(msg, "台车下降") == 0)
        {
//            app_eleact_run(-1);
            app_mc_run(LIFT_ID, -pallet_speed_value);
        }
        else if(strcmp(msg, "左轮前进") == 0)
        {
            app_brake_state_set(1);
            app_mc_run(LEFT_ID, 600);
            app_mc_run(RIGHT_ID, 0);
        }
        else if(strcmp(msg, "左轮后退") == 0)
        {
            app_brake_state_set(1);
            app_mc_run(LEFT_ID, -600);
            app_mc_run(RIGHT_ID, 0);
        }
        else if(strcmp(msg, "右轮前进") == 0)
        {
            app_brake_state_set(1);
            app_mc_run(LEFT_ID, 0);
            app_mc_run(RIGHT_ID, 600);
        }
        else if(strcmp(msg, "右轮后退") == 0)
        {
            app_brake_state_set(1);
            app_mc_run(LEFT_ID, 0);
            app_mc_run(RIGHT_ID, -600);
        }
        else if(strcmp(msg, "同时前进") == 0)
        {
            app_brake_state_set(1);
            app_mc_run(LEFT_ID, 600);
            app_mc_run(RIGHT_ID, 600);
        }
        else if(strcmp(msg, "同时后退") == 0)
        {
            app_brake_state_set(1);
            app_mc_run(LEFT_ID, -600);
            app_mc_run(RIGHT_ID, -600);
        }
        break;

    case MOVE_WIDGET:
        if(strcmp(msg, "切换到主界面") == 0)
        {
            app_force_move_disable();
//            cia402_set_output(LEFT_ID,0);
//            cia402_set_output(RIGHT_ID,0);
            app_brake_state_set(0);
            _widget = MAIN_WIDGET;
        }
        else if(strcmp(msg, "助力打开") == 0)
        {
            pallet_assit_status =1;
            app_force_move_enable();
        }
        else if(strcmp(msg, "助力关闭") == 0)
        {
            pallet_assit_status =0;
            app_force_move_disable();

        }
        break;
    case SET_WIDGET:
        if(strcmp(msg, "切换到主界面") == 0)
        {
            app_arm_disable();
            _widget = MAIN_WIDGET;
        }
        else if(strcmp(msg, "调整臂一键展开") == 0)
        {
            app_arm_run(UNFOLD_STORAGE, 0);
        }
        else if(strcmp(msg, "调整臂一键收缩") == 0)
        {
            app_arm_run(UNFOLD_STORAGE, 1);
        }
        else if(strcmp(msg, "调整臂上升") == 0)
        {
            app_arm_run(UP_DOWN, 0);
        }
        else if(strcmp(msg, "调整臂下降") == 0)
        {
            app_arm_run(UP_DOWN, 1);
        }
        else if(strcmp(msg, "调整臂向左") == 0)
        {
            app_arm_run(LEFT_RIGHT, 0);
        }
        else if(strcmp(msg, "调整臂向右") == 0)
        {
            app_arm_run(LEFT_RIGHT, 1);
        }
        else if(strcmp(msg, "调整臂伸出") == 0)
        {
            app_arm_run(IN_OUT, 0);
        }
        else if(strcmp(msg, "调整臂收缩") == 0)
        {
            app_arm_run(IN_OUT, 1);
        }
        else if(strcmp(msg, "调整臂俯") == 0)
        {
            app_arm_run(PITCH, 0);
        }
        else if(strcmp(msg, "调整臂仰") == 0)
        {
            app_arm_run(PITCH, 1);
        }
        else if(strcmp(msg, "调整臂左偏摆") == 0)
        {
            app_arm_run(YAW, 0);
        }
        else if(strcmp(msg, "调整臂右偏摆") == 0)
        {
            app_arm_run(YAW, 1);
        }
        else if(strcmp(msg, "调整臂左翻滚") == 0)
        {
            app_arm_run(ROLL, 0);
        }
        else if(strcmp(msg, "调整臂右翻滚") == 0)
        {
            app_arm_run(ROLL, 1);
        }
        break;
    case LOCKED_WIDGET:
        if(strcmp(msg, "切换到主界面") == 0)
        {
            _widget = MAIN_WIDGET;
//            pallet_locked = 0;//解除锁定
        }
        break;
    };
    LOG_D("widget = %d", _widget);


//    static const char* ack = "ack";
//    static uint8_t ack_param[] = {4,5,6};
//    static uint8_t* ack_par = ack_param;
//
//
//    *msg = (char**)&ack;
//    *param = (uint8_t**)&ack_par;
//    *param_len = 2;

    return true;
}


void app_dctft_init(void)
{
    dc_tft_init();
    dc_tft_reg_proc(proc);
}


void pallet_locked_ctr(void)
{
    if(_widget == MOVE_WIDGET)
    {
        app_force_move_disable();
//        cia402_set_output(LEFT_ID,0);
//        cia402_set_output(RIGHT_ID,0);
        app_brake_state_set(0);
        _widget = MAIN_WIDGET;
    }
}



static int pallet_speed_set(int argc, char *argv[])
{
    pallet_speed_value = abs(atoi(argv[1]));
    if(pallet_speed_value>3000)pallet_speed_value=3000;
    LOG_I("pallet_speed = %d", pallet_speed_value);
    return RT_EOK;
}
MSH_CMD_EXPORT(pallet_speed_set, 设置台车升降电机速度);

